﻿using System.Xml.Linq;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Base class for all other SharePoint Ribbon visual controls.
    /// </summary>
    public abstract class RibbonVisualControl : RibbonControl
    {
        private bool _enabled = true;

        /// <summary>
        /// Gets a value indicating whether the control is enabled.
        /// </summary>
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        /// <summary>
        /// Gets or sets the ToolTip displayed when the mouse pointer hovers over the control.
        /// </summary>
        public RibbonToolTip ToolTip { get; set; }

        /// <summary>
        /// Gets or sets the Image displayed in the control.
        /// </summary>
        public RibbonImageDefinition Image { get; set; }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonVisualControl.
        /// </summary>
        protected RibbonVisualControl() { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonVisualControl.
        /// </summary>
        /// <param name="id"></param>
        protected RibbonVisualControl(string id) : base(id) { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonVisualControl.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        protected RibbonVisualControl(string id, string title) : base(id, title) { }

        internal override XElement AsXElement()
        {
            var element = base.AsXElement();


            if (ToolTip != null)
            {
                if (!string.IsNullOrEmpty(ToolTip.Title))
                    element.Add(new XAttribute(Schema.ToolTipTitle, ToolTip.Title));
                if (!string.IsNullOrEmpty(ToolTip.Description))
                    element.Add(new XAttribute(Schema.ToolTipDescription, ToolTip.Description));
                if (ToolTip.Image != null)
                {
                    element.Add(new XAttribute(Schema.ToolTipImage32by32, ToolTip.Image.Url32));
                    if (ToolTip.Image.X.HasValue)
                        element.Add(new XAttribute(Schema.ToolTipImage32by32Left, ToolTip.Image.Position16X));
                    if (ToolTip.Image.Y.HasValue)
                        element.Add(new XAttribute(Schema.ToolTipImage32by32Top, ToolTip.Image.Position32X));

                }
            }

            if (Image != null)
            {
                if (Image.Url16 != null)
                    element.Add(new XAttribute(Schema.Image16by16, Image.Url16));
                if (Image.Url32 != null)
                    element.Add(new XAttribute(Schema.Image32by32, Image.Url32));

                if (Image.X.HasValue)
                {
                    if (Image.Url16 != null)
                        element.Add(new XAttribute(Schema.Image16by16Left, Image.Position16X));
                    if (Image.Url32 != null)
                        element.Add(new XAttribute(Schema.Image32by32Left, Image.Position32X));
                }

                if (Image.Y.HasValue)
                {
                    if (Image.Url16 != null)
                        element.Add(new XAttribute(Schema.Image16by16Top, Image.Position16Y));
                    if (Image.Url32 != null)
                        element.Add(new XAttribute(Schema.Image32by32Top, Image.Position32Y));
                }
            }
            return element;
        }
    }
}
